What is strip-json-comments?
The strip-json-comments package allows you to strip comments from JSON content, making it possible to use comments in JSON files or strings for development purposes without violating the JSON format specification. This can be particularly useful for configuration files or complex JSON data where comments can provide valuable context or instructions.
What are strip-json-comments's main functionalities?
Strip comments from JSON string
This feature allows you to remove both line (//) and block (/* */) comments from a JSON string, making it a valid JSON format. The code sample demonstrates how to use the package to strip comments from a JSON string containing both types of comments.
"use strict";\nconst stripJsonComments = require('strip-json-comments');\n\nconst jsonWithComments = '{\n\t// This is a comment\n\t"foo": true\n}';\nconst cleanedJson = stripJsonComments(jsonWithComments);\n\nconsole.log(cleanedJson); // {\n\t\"foo\": true\n}"
Other packages similar to strip-json-comments
json5
json5 is a package that extends the JSON format to include some ECMAScript 5.1 features, such as comments, trailing commas, and more. While it offers a broader scope of features beyond just stripping comments, it can be used for a similar purpose of handling JSON data with comments.
jsonc-parser
jsonc-parser is a parser that handles JSONC (JSON with Comments) format. It is designed to parse and manipulate JSONC data, offering functionality to strip comments as part of its parsing process. Compared to strip-json-comments, jsonc-parser provides a more comprehensive parsing and manipulation toolset for JSONC.
Strip comments from JSON. Lets you use comments in your JSON files!
This is now possible:
{
"unicorn": "cake"
}
It will remove single-line comments //
and multi-line comments /**/
.
Also available as a gulp/grunt/broccoli plugin.
There's also json-comments
, but it's only for Node.js, inefficient, bloated as it also minifies, and comes with a require
hook, which is :(
Install
$ npm install --save strip-json-comments
$ bower install --save strip-json-comments
$ component install sindresorhus/strip-json-comments
Usage
var json = '{/*rainbows*/"unicorn":"cake"}';
JSON.parse(stripJsonComments(json));
API
input
Type: string
Accepts a string with JSON and returns a string without comments.
CLI
$ npm install --global strip-json-comments
$ strip-json-comments --help
strip-json-comments input-file > output-file
strip-json-comments < input-file > output-file
Related
License
MIT © Sindre Sorhus